package com.adong.test;

import com.adong.config.ApplicationConfig;
import com.adong.config.ApplicationConfig2;
import com.adong.pojo.Car;
import com.adong.pojo.User;
import jdk.nashorn.internal.ir.CatchNode;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @author adong
 */


public class RegisterDemo {


    /**
     * 使用注解的方式来获取配置文件中注入的值
     */
    @Test
    public void xmlDemo() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        User user = (User) applicationContext.getBean("user", User.class);
        System.out.println(user);
    }

    /**
     * 在配置类当中添加@Bean组件,给User赋值
     */
    @Test
    public void annotationDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        User user = (User) applicationContext.getBean("user", User.class);
        System.out.println(user);
    }

    /**
     * 测试容器中有哪些组件
     */
    @Test
    public void ComponentScanDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);

        //输出 Spring 容器中所有 JavaBean 的名称
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }


    /**
     * 测试他们的bean的作用域
     */
    @Test
    public void scopeDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig2.class);
        User user1 = (User) applicationContext.getBean("user", User.class);
        User user2 = (User) applicationContext.getBean("user", User.class);
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user1 == user2);
    }


    /**
     * Conditional()的使用
     */
    @Test
    public void ConditionalDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig2.class);
        //通过类型获得bean的名字
        String[] namesForType = applicationContext.getBeanNamesForType(User.class);
        for (String name : namesForType) {
            System.out.println(name);
        }
        // Spring 容器中指定类型的所有 JavaBean 对象。
        Map<String, User> persons = applicationContext.getBeansOfType(User.class);
        System.out.println(persons);
    }

    /**
     * 快速导入一个组件使用:@import注解
     */
    @Test
    public void importDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig2.class);
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }

    /**
     * factory注册组件
     */
    @Test
    public void factoryBeanDemo() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig2.class);
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        Car car = applicationContext.getBean("factoryBean", Car.class);
        System.out.println("CarFactoryBean里面的类型 : " + car);
        Object bean = applicationContext.getBean("&factoryBean");
        System.out.println("获取工厂bean本身 : " + bean.getClass());

    }

}
